const {age, isNew, availableDescription, experience, currentCost, costDescription} = require('./utils.js')

module.exports = { 
	async parse(raw) {
        const person = { ...raw, age: age(raw.birthday), isNew: isNew(raw.createdDate) }
        const availability = { ...raw.availability, description: availableDescription(raw.availability.availableDate) }
        const profession = { ...raw.profession, experience: experience(raw.profession.professionStartDate) }
        const currentCostAmount = currentCost(raw.cost.plans)
        const cost = { ...raw.cost, amount: currentCostAmount, description: costDescription(currentCostAmount, raw.cost.unit) }
		const projects = raw.projects
		
		let projectIds = [];
		for(let i=0;i<projects.length;i++) {
			projectIds.push(projects[i].id);
		}
		let  { success, result: { data: _projects } } = await uniCloud.callFunction({
		    name: "getProjects",
		    data: { ids: projectIds }
		})
		for(let i=0; i< _projects.length; i++){
			_projects[i] = _projects[i] || {name: '涉密项目', description: '' };
			projects.forEach((project,index) => {
				if(project.id == _projects[i].id){
					projects[index] = { ...project, ..._projects[i] }
				}
			})
		}
		
        return { ...person, availability, profession, projects, cost }
    }
}
